package castledefense.gui;

import twl.global.widget.GlobalPanel;
import castledefense.data.GameData;
import castledefense.data.Resources;
import castledefense.game.render.Cam;
import castledefense.game.screens.MainGameScreen;
import castledefense.logic.Map;
import castledefense.logic.buildings.Building;
import castledefense.logic.buildings.Castle;
import castledefense.logic.buildings.Tower1;
import castledefense.logic.buildings.Tower2;
import castledefense.logic.units.Unit;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

import de.matthiasmann.twl.Event;
import de.matthiasmann.twl.GUI;

public class MinimapPanel extends GlobalPanel {

	public static final int cellSize = 3;
	public static final double cellSizeDouble = 3;
	private SpriteBatch spriteBatch;

	private GUIMainGame gui;
	private MainGameScreen state;

	// private Texture background;
	private TextureRegion rectangleVision;
	private TextureRegion castle;
	private TextureRegion buildingNormal;
	/*
	 * private Texture buildingFire; private Texture buildingWater; private
	 * Texture buildingEarth; private Texture buildingAir;
	 */
	private TextureRegion unitFire;
	private TextureRegion unitWater;
	private TextureRegion unitEarth;
	private TextureRegion unitAir;
	private Texture bigTexture;

	private int screenWidth = GameData.SCREENSIZEX * cellSize / Map.cellWidth;
	private int screenHeight = GameData.SCREENSIZEY * cellSize / Map.cellWidth;

	
	public MinimapPanel(GUIMainGame gui, MainGameScreen state) {
		this.state = state;
		this.gui = gui;

		setTheme("");

		setClip(true);

		// Create texture
		Pixmap pixmap;
		pixmap = new Pixmap(32, 16, Format.RGBA8888);

		// Castle 0,0 -16x16
		pixmap.setColor(Color.WHITE);
		pixmap.fillRectangle(0, 1, 5 * cellSize, 5 * cellSize);
		pixmap.setColor(Color.BLACK);
		pixmap.drawRectangle(0, 1, 5 * cellSize, 5 * cellSize);

		// normal building 16,0 -8x8
		pixmap.setColor(Color.WHITE);
		pixmap.fillRectangle(16, 2, 2 * cellSize, 2 * cellSize);
		pixmap.setColor(Color.BLACK);
		pixmap.drawRectangle(16, 2, 2 * cellSize, 2 * cellSize);

		// Unit fire 16,8, -2x-2
		pixmap.setColor(Color.RED);
		pixmap.fillRectangle(16, 8, 2, 2);

		// Unit water 16,10, -2x-2
		pixmap.setColor(Color.BLUE);
		pixmap.fillRectangle(16, 10, 2, 2);

		// Unit earth 18,8, -2x-2
		pixmap.setColor(0.4f, 0.2f, 0, 1);
		pixmap.fillRectangle(18, 8, 2, 2);

		// Unit air 18,10, -2x-2
		pixmap.setColor(1f, 1f, 0, 1f);
		pixmap.fillRectangle(18, 10, 2, 2);

		pixmap.setColor(1f, 1f, 1f, 0.8f);
		pixmap.drawPixel(16, 13);

		bigTexture = new Texture(pixmap);
		pixmap.dispose();

		castle = new TextureRegion(bigTexture, 0, 0, 16, 16);
		buildingNormal = new TextureRegion(bigTexture, 16, 0, 8, 8);

		unitFire = new TextureRegion(bigTexture, 16, 8, 2, 2);
		unitWater = new TextureRegion(bigTexture, 16, 10, 2, 2);
		unitEarth = new TextureRegion(bigTexture, 18, 8, 2, 2);
		unitAir = new TextureRegion(bigTexture, 18, 10, 2, 2);

		rectangleVision = new TextureRegion(bigTexture, 16, 13, 1, 1);

	}

	public GUIMainGame getStateGUI() {
		return gui;
	}

	private boolean leftButton = false;;

	@Override
	protected boolean handleEvent(Event evt) {
		if (evt.getType() == Event.Type.MOUSE_BTNDOWN) {
			if (evt.getMouseButton() == Event.MOUSE_LBUTTON) {
				leftButton = true;
				int x = (evt.getMouseX() - getX()) * Map.cellWidth / cellSize;
				int y = (MainGameScreen.mapSizeX * Map.cellWidth)
						- ((evt.getMouseY() - getY()) * Map.cellWidth / cellSize);
				Cam.setPosition(x, -y);

			}
		}
		if (evt.getType() == Event.Type.MOUSE_BTNUP) {
			if (evt.getMouseButton() == Event.MOUSE_LBUTTON) {
				leftButton = false;
				gui.setDragging(false);
			}
		}
		if (evt.getType() == Event.Type.MOUSE_EXITED) {
			leftButton = false;
		}
		if (evt.getType() == Event.Type.MOUSE_DRAGGED) {
			if (leftButton) {
				gui.setDragging(true);
				int x = (evt.getMouseX() - getX()) * Map.cellWidth / cellSize;
				int y = (MainGameScreen.mapSizeX * Map.cellWidth)
						- ((evt.getMouseY() - getY()) * Map.cellWidth / cellSize);
				Cam.setPosition(x, -y);
			}
		}
		return true;
	}

	public void paintWidget(GUI gui) {
		render();
	}

	public void render() {

		Map map = state.getMap();
		int mapX = getX();
		int mapY = getY();

		spriteBatch = state.getSpriteBatch();

		spriteBatch.draw(Resources.grassIm, mapX, mapY, MainGameScreen.mapSizeX * cellSize,
				MainGameScreen.mapSizeY * cellSize);
		// Draw all buildings
		for (Building b : map.getBuildingList()) {
			int posX = mapX + b.getCellX() * cellSize;
			int posY = mapY + (MainGameScreen.mapSizeY - b.getCellY()) * cellSize;
			posY -= b.getSize() * cellSize;
			if (b instanceof Castle) {
				spriteBatch.draw(castle, posX, posY);
			} else if (b instanceof Tower1) {
				spriteBatch.draw(buildingNormal, posX, posY);
			} else if (b instanceof Tower2) {
				spriteBatch.draw(buildingNormal, posX, posY);
			}
		}

		// Draw all units
		
		for (Unit unit : map.getUnitList()) {
			float posX = (float) (mapX + (unit.getCenterX() * cellSizeDouble));
			float posY = (float) (mapY + ((MainGameScreen.mapSizeY - unit.getCenterY()) * cellSizeDouble));
			posY -= 2;
			if (unit.getType() == GameData.fire) {
				spriteBatch.draw(unitFire, posX, posY);
			} else if (unit.getType() == GameData.water) {
				spriteBatch.draw(unitWater, posX, posY);
			} else if (unit.getType() == GameData.earth) {
				spriteBatch.draw(unitEarth, posX, posY);
			} else if (unit.getType() == GameData.air) {
				spriteBatch.draw(unitAir, posX, posY);
			}
		}

		// Draw visible zone
		final float screenX = ((float) mapX + (Cam.getCameraBottomLeftX()
				* (float) cellSize / (float) Map.cellWidth));
		final float screenY = ((float) mapY + ((-Cam.getCameraBottomLeftY()
				+ MainGameScreen.mapSizeY * (float) Map.cellWidth - GameData.SCREENSIZEY)
				* cellSize / Map.cellWidth));
		final float x1 = (screenWidth * (Cam.camera.zoom) - screenWidth) / 2;
		final float y1 = (screenHeight * (Cam.camera.zoom) - screenHeight) / 2;
		final float x2 = x1 * 2f;
		final float y2 = y1 * 2f;
		final float lineWidth = 2f;
		spriteBatch.draw(rectangleVision, screenX - x1, screenY - y1,
				lineWidth, screenHeight + y2);
		spriteBatch.draw(rectangleVision, screenX + screenWidth + x1, screenY
				- y1, lineWidth, screenHeight + y2);
		spriteBatch.draw(rectangleVision, screenX - x1, screenY - y1,
				screenWidth + x2, lineWidth);
		spriteBatch.draw(rectangleVision, screenX - x1, screenY + screenHeight
				+ y1, screenWidth + x2 + lineWidth, lineWidth);
	}
}
